# -- coding: utf-8 --
import os



from utils.base_util import DBConnection, CommonTools


class Report:

    @classmethod
    def generate_html_report(cls, table, version):
        """
        生成规定模板的测试报告
        :param table: 结果表的表名
        :param version: 版本号
        :return: None
        """
        # 读取模板文件
        with open('template.html', encoding='utf8') as file:
            content = file.read()
        # 读取数据库该版本的所有数据
        sql = f'select * from {table} where version_id="{version}"'
        result = DBConnection.query(sql, 'test_db')
        date = CommonTools.get_ltime('%Y-%m-%d %H:%M:%S')
        base_sql = f'select count(test_result) from {table} where test_result='
        suc_count_sql = base_sql + '"pass"'
        fail_count_sql = base_sql + '"fail"'
        error_count_sql = base_sql + '"error"'

        suc_count = DBConnection.query(suc_count_sql, 'test_db')[0][0]
        # suc_count = DBTools.query('result_db', suc_count_sql)[0][0]
        fail_count = DBConnection.query(fail_count_sql, 'test_db')[0][0]
        # fail_count = DBTools.query('result_db', fail_count_sql)[0][0]
        error_count = DBConnection.query(error_count_sql, 'test_db')[0][0]
        # error_count = DBTools.query('result_db', error_count_sql)[0][0]
        last_time = CommonTools.get_ltime('%Y-%m-%d %H:%M:%S')

        content = content.replace('$test-date', date)
        content = content.replace('$test-version', version)
        content = content.replace('$pass-count', str(suc_count))
        content = content.replace('$fail-count', str(fail_count))
        content = content.replace('$error-count', str(error_count))
        content = content.replace('$last-time', last_time)

        test_result = ''
        for record in result:
            if record[6] == 'pass':
                color = 'lightgreen'
            elif record[6] == 'fail':
                color = 'red'
            else:
                color = 'yellow'
            screenshot = '无'
            # if record[7] != '':
            #     temp = str(record[-1]).split('\\')
            #     img_path = "./" + temp[-2] + "/" + temp[-1]
            #     screenshot = f'<a href="{img_path}">查看截图</a>'
            # else:
            #     screenshot = '无'
            test_result += f'<tr height="40">' \
                           f'<td width="7%">{record[0]}</td>' \
                           f'<td width="9%">{record[2]}</td>' \
                           f'<td width="7%">{record[4]}</td>' \
                           f'<td width="7%">{record[3]}</td>' \
                           f'<td width="10%">{record[5]}</td>' \
                           f'<td width="7%" bgcolor="{color}">{record[6]}</td>' \
                           f'<td width="15%">{record[8]}</td>' \
                           f'<td width="15%">{record[7]}</td>' \
                           f'<td width="10%">{screenshot}</td>' \
                           f'</tr>\r\n'
        content = content.replace('$test-result', test_result)
        # 将更新后的内容写入规定的文件中
        report_path = f'..\\report\\{version}\\'
        if not os.path.exists(report_path) :
            os.makedirs(report_path)
        report_name = f'{report_path}{version}_report.html'
        with open(report_name, 'w', encoding='utf8') as file:
            file.write(content)

if __name__ == '__main__':
    Report.generate_html_report('sharedParkingTestResult', '4')